/*
 * @Author: 何 海深 813936564@qq.com
 * @Date: 2022-11-24 16:01:14
 * @LastEditors: 何 海深 813936564@qq.com
 * @LastEditTime: 2023-11-30 16:31:35
 * @FilePath: \datashowapicture\src\hooks\commonFunction\commonFunctionIndex.ts
 * @Description: 公共方法
 */
const commonFunctionHook = () => {
  /**
   * @description: 设置表格索引
   * @param {Array} params
   * @param {number} currentPage
   * @param {number} pageSize
   * @return {void}
   */
  const setTableKeyIndex = async (
    params: any[],
    currentPage?: number | undefined,
    pageSize?: number | undefined
  ) => {
    let baseList = 1
    if (currentPage && pageSize) {
      baseList = (currentPage - 1) * pageSize
    }
    params.forEach((i: any, k: any) => (i.indexKeys = k + 1 + baseList))
  }
  /**
   * @description: 将文字转换成base64图片
   * @param {string} name
   * @param {number} width
   * @param {number} height
   * @return {*}
   */
  const textToImage = (name: string, width: number, height: number) => {
    const fsize = getCurrentTextPx(30)
    // const fontSize = 120;
    const fontWeight = 'normal'
    const canvas: any = document.getElementById('head_canvas_default')
    // const img1 = document.getElementById('head_canvas_default')
    canvas.width = width
    canvas.height = height
    const context = canvas.getContext('2d')
    context.fillStyle = 'transparent'
    context.fillRect(0, 0, canvas.width, canvas.height)
    context.fillStyle = '#d3d3d3'
    context.font = fontWeight + ' ' + fsize + 'px sans-serif'
    context.textAlign = 'left'
    context.textBaseline = 'top'
    context.fillText(name, 0, 0)
    return canvas.toDataURL('image/png')
  }
  /**
   * @description:转换文字的大小
   * @return {number}
   */
  const getCurrentTextPx = (currTextPx: number) => {
    const baseH = 1080
    const wh: number = window.innerHeight
    const currentHeight: number = wh / baseH
    return currTextPx * currentHeight
  }
  return {
    setTableKeyIndex,
    textToImage,
    getCurrentTextPx
  }
}
export default commonFunctionHook
